//
//  ScribbleManager.m
//  Test
//
//  Created by 杨德龙 on 2017/9/26.
//  Copyright © 2017年 ___杨德龙___. All rights reserved.
//

#import "ScribbleManager.h"
#import "OpenScribbleCommand.h"

#define kScribbleDataPath [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/data"]
#define kScribbleThumbnailPath [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/thumbnails"]

@interface ScribbleManager()

- (NSString *)scribbleDataPath;

- (NSString *)scribbleThumbnailPath;

- (NSArray *)scribbleDataPaths;

- (NSArray *)scribbleThumbnailPaths;

@end

@implementation ScribbleManager

// 保存 scribble 和 image
- (void) saveScribble:(Scribble *)scribble thumbnail:(UIImage *)image
{
    NSInteger newIndex = [self numberOfScribbles] + 1;
    
    // use the index as part of the name for each of them
    NSString *scribbleDataName = [NSString stringWithFormat:@"data_%ld", (long)newIndex];
    NSString *scribbleThumbnailName = [NSString stringWithFormat:@"thumbnail_%ld.png",
                                       (long)newIndex];
    
    // get a memento from the scribble
    // then save the memento in the file system
    ScribbleMemento *scribbleMemento = [scribble scribbleMemento];
    NSData *mementoData = [scribbleMemento data];
    NSString *mementoPath = [[self scribbleDataPath]
                             stringByAppendingPathComponent:scribbleDataName];
    [mementoData writeToFile:mementoPath atomically:YES];
    
    // save the thumbnail directly in
    // the file system
    NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    NSString *imagePath = [[self scribbleThumbnailPath]
                           stringByAppendingPathComponent:scribbleThumbnailName];
    [imageData writeToFile:imagePath atomically:YES];
}

- (NSInteger)numberOfScribbles
{
    NSArray *scribbleDataPathArray = [self scribbleDataPaths];
    return [scribbleDataPathArray count];
}

- (Scribble *)scribbleAtIndex:(NSInteger)index
{
    Scribble *loadedScribble = nil;
    NSArray *scribbleDataPathsArray = [self scribbleDataPaths];
    
    // load scribble data from the path indicated
    // by the index
    NSString *scribblePath = [scribbleDataPathsArray objectAtIndex:index];
    if (scribblePath)
    {
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSData *scribbleData = [fileManager contentsAtPath:[kScribbleDataPath
                                                            stringByAppendingPathComponent:
                                                            scribblePath]];
        ScribbleMemento *scribbleMemento = [ScribbleMemento mementoWithData:scribbleData];
        loadedScribble = [Scribble scribbleWithMemento:scribbleMemento];
    }
    
    return loadedScribble;
}

- (UIView *) scribbleThumbnailViewAtIndex:(NSInteger)index
{
    ScribbleThumbnailViewImageProxy *loadedScribbleThumbnail = nil;
    NSArray *scribbleThumbnailPathsArray = [self scribbleThumbnailPaths];
    NSArray *scribblePathsArray = [self scribbleDataPaths];
    
    // load scribble thumbnail from the path indicated
    // by the index
    NSString *scribbleThumbnailPath = [scribbleThumbnailPathsArray objectAtIndex:index];
    NSString *scribblePath = [scribblePathsArray objectAtIndex:index];
    
    if (scribbleThumbnailPath)
    {
        // initialize an instance of ScribbleThumbnailProxy
        // with the exact location of the thumbnail in the file system
        loadedScribbleThumbnail = [[ScribbleThumbnailViewImageProxy alloc] init];
        
        [loadedScribbleThumbnail setImagePath:[kScribbleThumbnailPath
                                               stringByAppendingPathComponent:
                                               scribbleThumbnailPath]];
        [loadedScribbleThumbnail setScribblePath:[kScribbleDataPath
                                                  stringByAppendingPathComponent:
                                                  scribblePath]];
        
        
        // assign a touch command to the scribble thumbnail
        // so it can be used to open a scribble by touch
        OpenScribbleCommand *touchCommand = [[OpenScribbleCommand alloc]
                                             initWithScribbleSource:loadedScribbleThumbnail];
        [loadedScribbleThumbnail setTouchCommand:touchCommand];
    }
    
    return loadedScribbleThumbnail;
}



#pragma mark ---- Private Method
// 主要是 文件目录 和 文件目录下的文件图片 和 scribble 
- (NSString *)scribbleDataPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:kScribbleDataPath])
    {
        // 如果没有 这个目录那么创建一个目录
        [[NSFileManager defaultManager] createDirectoryAtPath:kScribbleDataPath withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    
    return kScribbleDataPath;
}

- (NSString *)scribbleThumbnailPath
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:kScribbleThumbnailPath]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:kScribbleThumbnailPath withIntermediateDirectories:YES attributes:nil error:NULL];
    }
    return kScribbleThumbnailPath;
}


- (NSArray *)scribbleDataPaths
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *scribbleDataPathsArray = [fileManager contentsOfDirectoryAtPath:[self scribbleDataPath]
                                                                       error:&error];
    
    return scribbleDataPathsArray;
}


- (NSArray *)scribbleThumbnailPaths
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *scribbleThumbnailPathsArray = [fileManager contentsOfDirectoryAtPath:[self scribbleThumbnailPath]
                                                                            error:&error];
    return scribbleThumbnailPathsArray;
}



@end




























































































